package com.inxedu.os.edu.controller.main;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.inxedu.os.edu.pojo.SysFunction;
import com.inxedu.os.edu.service.main.SysFunctionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
@Api(tags = "主界面")
@Controller
@RequestMapping("/admin/")

public class MainController {




    private static Gson gson =(new GsonBuilder()).setDateFormat("yyyy-MM-dd HH:mm:ss").create();
    private List<SysFunction> functionList =null;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    private SysFunctionService sysFunctionService;
    /**
     *
     * @return 主界面
     */
    @RequestMapping(value = "main",method = RequestMethod.GET)
    @ApiOperation(value = "处理权限",notes = "返回主界面")
    public String toMainMain(HttpServletRequest request,Model model){
//        //缓存
//        List<SysFunction> userFunctionList =(List<SysFunction>) stringRedisTemplate.opsForValue().get()
        //权限
//        String user = stringRedisTemplate.opsForValue().get("user");
//        SysUser sysUser = JsonUtils.jsonToPojo(user, SysUser.class);
            int userId = (int) request.getSession().getAttribute("userId");

//        System.out.println("------------------------"+userId);

        List<SysFunction> userFunctionList = sysFunctionService.querySysUserFunction(userId);
        System.out.println(userFunctionList);
        functionList=processingParentAuthority(userFunctionList);
        model.addAttribute("userFunctionList",functionList);
        return "view/inxedu/admin/main/main";
    }


    /**
     * 处理父权限
     */
    private List<SysFunction> processingParentAuthority(List<SysFunction> userFunctionList){
        List<SysFunction> parentList=new ArrayList<>();
        List<SysFunction> list =null;
        if(!userFunctionList.isEmpty()){
        for(SysFunction s : userFunctionList){
           if(s.getParentId()==0){
               parentList.add(s);
           }
        }
            System.out.println(parentList);
            for(SysFunction sf : parentList){
                processingChildAuthority(userFunctionList,sf);
            }
            list=parentList;
        }
        return list;
    }
    /**
     * 处理子权限
     */
    private  void processingChildAuthority(List<SysFunction> userFunctionList, SysFunction sf){
            List<SysFunction> childList=new ArrayList<>();
            for(SysFunction ss :userFunctionList ){
                if(sf.getFunctionId()==ss.getParentId()&& ss.getFunctionType()==1){
                    childList.add(ss);
//                    processingChildAuthority(userFunctionList,ss);
                }

            }
             System.out.println(childList);
            sf.setChildList(childList);
//            functionList.add(sf);
//        List<SysFunction> childFunction = new ArrayList<SysFunction>();
//        for(SysFunction usf : userFunctionList){
//            if(sf.getFunctionId()==usf.getParentId() && usf.getFunctionType()==1){
//                childFunction.add(usf);
//
//            }
//        }
//        System.out.println(childFunction);
//
//        sf.setChildList(childFunction);
//        System.out.println(sf);
//        functionList.add(sf);
    }
    /**
     *
     * @return 返回index界面
     */
/*    @RequestMapping("main/index")
    public String toIndex(){

        return "/admin/main/index";
    }*/
    @RequestMapping(value = "main/notfunction",method = RequestMethod.GET)
    @ApiOperation(value = "返回index界面")
    public ModelAndView notFunctionMsg(){
        ModelAndView model = new ModelAndView();
        model.setViewName("view/common/notFunctonMsg");
        return model;
    }
}
